<template>
  <div class="customer-container">
    <el-card>
      <el-form :inline="true"
               ref="search_data"
               class="demo-form-inline">
        <el-row>
          <el-form-item label="客户">
            <el-input v-model="search_data.title"
                      placeholder="输入客户名称" />
          </el-form-item>
        </el-row>
      </el-form>
    </el-card>

    <el-card shadow="never"
             style="margin-top:10px;">
      <div class="main-card">
        <div>
          <span style="font-size:18px;font-weight:900;">客户列表</span>
        </div>
        <div>
          <el-button icon="el-icon-plus"
                     type="primary"
                     @click="handleAdd()">新增</el-button>
        </div>
      </div>
      <el-table :data="table_data"
                tooltip-effect="dark"
                style="width: 100%">
        <el-table-column prop="title"
                         label="名称">
        </el-table-column>
        <el-table-column prop="type_title"
                         label="类型">
        </el-table-column>
        <el-table-column prop="phone"
                         label="电话">
        </el-table-column>
        <el-table-column prop="address"
                         label="地址">
        </el-table-column>
        <el-table-column prop="created_at"
                         label="创建日期">
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog title="新增/编辑"
               :visible.sync="dialogFormVisible"
               width="500px">
      <el-form :model="form"
               label-width="80px">
        <el-form-item label="名称">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="form.type"
                     placeholder="请选择客户类型">
            <el-option :key="1"
                       label="个人"
                       :value="1">
            </el-option>
            <el-option :key="2"
                       label="单位"
                       :value="2">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.address"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="handleAddData">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getList, addData } from '@/api/customer'
export default {
  data () {
    return {
      search_data: {
        page: 1,
        limit: 10,
        title: '',
      },
      form: {
        id: '',
        title: '',
        type: 1,
        phone: '',
        address: '',
      },
      all_count: 0,
      table_data: [],
      dialogFormVisible: false,
    }
  },

  created () {
    this.fetchData()
  },
  methods: {
    fetchData () {
      getList(this.search_data).then(response => {
        this.table_data = response.data.data
        this.all_count = response.data.count
      })
    },
    onSearch () { },
    handleAdd () {
      this.dialogFormVisible = true
    },

    handleAddData () {
      if (this.form.id) {
        editData(this.form).then(response => {
          if (response.code == 1000) {
            this.$message({
              message: '数据保存成功',
              type: 'success'
            });
            this.dialogFormVisible = false
          } else {
            this.$message.error(response.msg);
          }
        })
      } else {
        addData(this.form).then(response => {
          if (response.code == 1000) {
            this.$message({
              message: '数据添加成功',
              type: 'success'
            });
            this.dialogFormVisible = false
            this.fetchData()
          } else {
            this.$message.error(response.msg);
          }
        })
      }
    },
  }
}
</script>
<style lang="scss" scoped>
.customer-container {
  width: 1200px;
  margin: 20px auto;
  margin-top: 50px;
  border-radius: 10px;
  background-color: #fff;
  color: #333;
  .main-card {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>
